package com.cc.bubbleanimation;

/*
 * AnimTools.java
 * classes : com.videoview.activity.AnimTools
 * @author Jun.w
 * V 1.0.0
 * Create at 2015��5��7�� ����5:25:35
 */

import com.cc.bubbleanimation.application.BBApplication;

import android.animation.Animator.AnimatorListener;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;

/**
 * com.videoview.activity.AnimTools
 * 
 * @author Jun.w <br/>
 *         create at 2015��5��7�� ����5:25:35
 */
public class AnimTools {
  private static final String TAG = "AnimTools";

  /**
   * ��ֱ���¶�������ҪApi11���ϣ������������nineoldanimation
   * 
   * @param view
   */
  public static void openAnim(final View view, long time, AnimatorListener listener) {

    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(w, h);
    int height = view.getMeasuredHeight();
    if (view instanceof BubbleView) {
      height = ((BubbleView) view).getRadius();
    }
//    int width = view.getMeasuredWidth();
    final ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", 0.0f, 1f).setDuration(3000);// 渐变动画
    alpha.start();

    ObjectAnimator.ofFloat(view, "rotationY", 0.0f, 180f).setDuration(4000).start();// 渐变动画
    ValueAnimator animator = ValueAnimator.ofFloat(height * -1, BBApplication.getInstance().getScreenHeight());
    animator.setTarget(view);
    animator.setInterpolator(new AnticipateOvershootInterpolator());
    animator.setDuration(time).start();
    // animator.setInterpolator(value)
    animator.setRepeatCount(Integer.MAX_VALUE);
    animator.setRepeatMode(ValueAnimator.RESTART);
    animator.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        view.setTranslationY((Float) animation.getAnimatedValue());
      }
    });
      animator.addListener(new AnimatorListener() {
        
        @Override
        public void onAnimationStart(Animator animation) {
        }
        
        @Override
        public void onAnimationRepeat(Animator animation) {
          alpha.start();
          ObjectAnimator.ofFloat(view, "rotationY", 0.0f, 180f).setDuration(4000).start();// 渐变动画
        }
        
        @Override
        public void onAnimationEnd(Animator animation) {
        }
        
        @Override
        public void onAnimationCancel(Animator animation) {
        }
      });
  }

  public static void endAnim(final View view, long time, AnimatorListener listener) {

    int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    view.measure(w, h);
    int height = view.getMeasuredHeight();
    int width = view.getMeasuredWidth();
    Log.i("CC", "ViewHeight:" + height);
    ValueAnimator animator = ValueAnimator.ofFloat(height / 2, height * 2 * -1);
    animator.setTarget(view);
    animator.setInterpolator(new AnticipateOvershootInterpolator());
    animator.setDuration(time).start();
    // animator.setInterpolator(value)
    animator.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
        view.setTranslationY((Float) animation.getAnimatedValue());
      }
    });
    if (listener != null)
      animator.addListener(listener);
  }

}
